package com.example.singleton.demo.singleton;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 用Enum实现示例
 */
public class EnumModelSingleton {

    public static Connection getInstance() {
        return EnumSingleton.SingletonFactory.getConnection();
    }

    public enum EnumSingleton {
        SingletonFactory;
        private Connection connection;

        //枚举
        private EnumSingleton() {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "root", "111111");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public Connection getConnection() {
            return connection;
        }
    }
}
